const app = getApp()
import {
  getXzqList,
  getInfo
} from "../../utils/request"
import {
  getJsdList
} from "../../utils/request"
import {
  getJSinfo
} from "../../utils/request"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    objectArray: {},
    jsdobjectArray: {},
    showtext: false, //显示textarea
    name: '',
    phone: '',
    count: '',
    area: '',
    address: '',
    donate_school_name: '',
    index: 0,
    flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
    this.jsdList();
    this.getinfo()
  },
  // 获取个人信息
  async getinfo() {
    const data = await getInfo();
    this.setData({
      name: data.data.real_name,
    })
  },
  // 行政区域
  async getList() {
    const data = await getXzqList({type:1});
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({
        id: key,
        name: val.area
      })
    })
    this.setData({
      objectArray: {
        title: "请选择行政区域",
        array
      }
    })
  },
  // 捐书地点
  async jsdList() {
    const data = await getJsdList();
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({
        id: val.id,
        name: val.name
      })
    })
    this.setData({
      jsdobjectArray: {
        title: "请选择捐书地点",
        array
      }
    })
  },
  // 提交页面跳转
  postList: app.func.throttle(async function (obj) {
    const data = await getJSinfo([...obj][0]);
    if (data.code !== '1') return
    app.func.Toast.success("提交成功")
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/jszhengshu/index',
      })
    }, 1000)
  }),

  // 隐藏textarea
  showmodel(e) {
    this.setData({
      showtext: e.detail
    })
  },
  // 页面提交数据
  updataname(e) {
    this.setData({
      name: e.detail.value
    })
  },
  updataphone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  updatacount(e) {
    this.setData({
      count: e.detail.value
    })
  },
  updataaddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  updataQy(e) {
    this.setData({
      area: e.detail,
      showtext: false
    })
  },
  updataJsd(e) {
    this.setData({
      donate_school_name: e.detail,
      showtext: false
    })
  },
  async updatalist() {
    if (this.data.num == 1) {
      const data = {
        name: this.data.name,
        mobile: this.data.phone,
        count: this.data.count,
        area: this.data.area,
        donate_school_name: this.data.donate_school_name,
        type: this.data.num
      }
      this.postList(data)

    } else {
      const data = {
        name: this.data.name,
        mobile: this.data.phone,
        count: this.data.count,
        area: this.data.area,
        address: this.data.address,
        type: this.data.num
      }
      this.postList(data);
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  menuClick: function (e) {
    this.setData({
      num: e.currentTarget.dataset.num
    })
  },
})